package com.atguigu.controller;

import com.atguigu.constant.Constant;
import com.atguigu.pojo.User;
import com.atguigu.service.UserService;
import com.atguigu.service.impl.UserServiceImpl;
import com.atguigu.utils.MD5Util;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

/**
 * @version 1.0
 * @Author 马冰
 * @Date 2024/12/11 15:33
 * @注释 用户修改密码操作
 */
@WebServlet("/user/update")
public class AlterPassword extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取表单资源
        String oldPassword = req.getParameter("oldPassword");
        String newPassword = req.getParameter("newPassword");
        //获取session对象,并读取数据
        HttpSession session = req.getSession();
        User user = (User)session.getAttribute(Constant.SESSION_USER);
        //对获取的密码进行比较
        if (MD5Util.encrypt(oldPassword).equals(user.getPassword())) {
            UserService userService = new UserServiceImpl();
            userService.updatePasswordByid(user.getId(),MD5Util.encrypt(newPassword));
            //跳转到登录界面
            resp.sendRedirect("/login.html");
        }else {
            resp.sendRedirect("/updatePasswordError.html");
        }









    }
}
